Api 测试
1 | package ZooKeeperApi; |
运行结果:
1 | ZooKeeper connected, sessionId=99251216681467907 |
小结
ZooKeeper 不支持递归创建,即无法在父节点不存在的情况下创建一个子节点。另外,如果一个节点已经存在了,那么创建同名节点的时候会抛出
NodeExistsException异常。目前,ZooKeeper 的节点内容只支持字节数组(
byte[])类型。在同步接口调用中,我们需要关注接口抛出异常的可能,但是在异步接口中,所有的异常再回调函数中通过 Result Code 来体现。
org.apache.zookeeper.KeeperException.Code1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59public static enum Code implements CodeDeprecated {
/** Everything is OK */
OK (Ok),
/** System and server-side errors.
* This is never thrown by the server, it shouldn't be used other than
* to indicate a range. Specifically error codes greater than this
* value, but lesser than {@link #APIERROR}, are system errors.
*/
SYSTEMERROR (SystemError),
/** A runtime inconsistency was found */
RUNTIMEINCONSISTENCY (RuntimeInconsistency),
/** A data inconsistency was found */
DATAINCONSISTENCY (DataInconsistency),
/** Connection to the server has been lost */
CONNECTIONLOSS (ConnectionLoss),
/** Error while marshalling or unmarshalling data */
MARSHALLINGERROR (MarshallingError),
/** Operation is unimplemented */
UNIMPLEMENTED (Unimplemented),
/** Operation timeout */
OPERATIONTIMEOUT (OperationTimeout),
/** Invalid arguments */
BADARGUMENTS (BadArguments),
/** API errors.
* This is never thrown by the server, it shouldn't be used other than
* to indicate a range. Specifically error codes greater than this
* value are API errors (while values less than this indicate a
* {@link #SYSTEMERROR}).
*/
APIERROR (APIError),
/** Node does not exist */
NONODE (NoNode),
/** Not authenticated */
NOAUTH (NoAuth),
/** Version conflict */
BADVERSION (BadVersion),
/** Ephemeral nodes may not have children */
NOCHILDRENFOREPHEMERALS (NoChildrenForEphemerals),
/** The node already exists */
NODEEXISTS (NodeExists),
/** The node has children */
NOTEMPTY (NotEmpty),
/** The session has been expired by the server */
SESSIONEXPIRED (SessionExpired),
/** Invalid callback specified */
INVALIDCALLBACK (InvalidCallback),
/** Invalid ACL specified */
INVALIDACL (InvalidACL),
/** Client authentication failed */
AUTHFAILED (AuthFailed),
/** Session moved to another server, so operation is ignored */
SESSIONMOVED (-118),
/** State-changing request is passed to read-only server */
NOTREADONLY (-119);
}在更新数据时,
version参数使用 -1 表示针对最新版本进行更新。无论节点是否存在,通过调用
exists接口都可以注册 Watcher,能够对节点创建、节点删除和节点数据更新事件进行监听,但不监听子节点的各种变化。